/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinotrack.struts;

import com.dinotrack.domain.Dinotracker;
import com.dinotrack.domain.DinotrackerSQL;
import com.dinotrack.domain.Persona;
import com.dinotrack.domain.PersonaSQL;
import com.dinotrack.domain.Usuario;
import com.dinotrack.domain.UsuarioSQL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Albertina Durante, Matías Martínez Blanquier, Flavio Peralta, Jéssica Luz Villarreal.
 */
public class RegisterAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String CONTROL_PANEL = "controlPanel";
    private static final String INDEX = "index";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (request.getParameter("panelControl") != null) {
            return mapping.findForward(CONTROL_PANEL);
        }

        if (request.getParameter("index") != null) {
            return mapping.findForward(INDEX);
        }

        RegisterForm registerForm = (RegisterForm) form;
        Persona persona = new Persona();
        Usuario usuario = new Usuario();
        Dinotracker dinotracker = new Dinotracker();
        boolean error = false;


        if ((registerForm.getUsuario() == null) || registerForm.getUsuario().equals("")) {
            registerForm.setMensaje("nombre de usuario");
            error = true;
        }

        if ((registerForm.getContrasenia() == null) || (registerForm.getContrasenia().equals(""))) {
            registerForm.setMensaje("contraseña");
            error = true;
        }

        if ((registerForm.getReContrasenia() == null) || (registerForm.getReContrasenia().equals(""))) {
            registerForm.setMensaje("reingreso de contraseña");
            error = true;
        }

        if ((registerForm.getNombre() == null) || registerForm.getNombre().equals("")) {
            registerForm.setMensaje("nombre");
            error = true;
        }

        if ((registerForm.getApellido() == null) || registerForm.getApellido().equals("")) {
            registerForm.setMensaje("apellido");
            error = true;
        }

        if ((registerForm.getEmail() == null) || (registerForm.getEmail().equals("")) || registerForm.getEmail().indexOf("@") == -1) {
            registerForm.setMensaje("email");
            error = true;
        }

        if (error) {
            registerForm.setTipoMensaje(RegisterForm.ERROR_MESSAGE);
            return mapping.findForward(FAILURE);
        }

        BeanUtils.copyProperties(usuario, registerForm);
        persona.setUsuario(usuario);
        persona.setNombre(registerForm.getNombre());
        persona.setApellido(registerForm.getApellido());
        persona.setEmail(registerForm.getEmail());

        UsuarioSQL usuarioSQL = new UsuarioSQL();
        boolean aux = usuarioSQL.registrarUsuario(usuario);

        if (aux) {

            boolean registro = false;

            PersonaSQL personaSQL = new PersonaSQL();
            registro = personaSQL.registrarPersona(persona);

            if (registro) {
                personaSQL = new PersonaSQL();
                Persona personaAux = personaSQL.getPersona(usuario.getUsuario());

                if (personaAux != null) {
                    persona.setIdPersona(personaAux.getIdPersona());

                    DinotrackerSQL dinotrackerSQL = new DinotrackerSQL();
                    dinotrackerSQL.registrarDinotracker(persona);

                    registerForm.setTipoMensaje(RegisterForm.SUCCESS_MESSAGE);
                } else {
                    return mapping.findForward(FAILURE);
                }
            } else {
                return mapping.findForward(FAILURE);
            }

        } else {
            registerForm.setTipoMensaje(RegisterForm.DB_ERROR_MESSAGE);
            return mapping.findForward(FAILURE);
        }

        return mapping.findForward(SUCCESS);
    }
}
